package com.chushouya.order.service.api.impl;

import com.chushouya.common.support.Contexts;
import com.chushouya.order.dao.entity.OrderClothesProductEntity;
import com.chushouya.order.dao.repository.OrderClothesProductRepository;
import com.chushouya.order.dto.api.order.*;
import com.general.framework.core.exception.Ex;
import com.general.framework.core.lang.Beans;
import com.general.framework.core.lang.Check;
import com.general.framework.core.lang.Jsons;
import com.general.framework.core.schema.PageResult;
import com.chushouya.order.constants.BizTypeEnum;
import com.chushouya.order.constants.UserOrderListLogicStatusEnum;
import com.chushouya.order.dao.entity.OrderAddressEntity;
import com.chushouya.order.dao.entity.OrderEntity;
import com.chushouya.order.dao.entity.OrderProductEntity;
import com.chushouya.order.dao.repository.OrderAddressRepository;
import com.chushouya.order.dao.repository.OrderProductRepository;
import com.chushouya.order.dao.repository.OrderRepository;
import com.chushouya.order.dto.admin.order.OrderListApiDTO;
import com.chushouya.order.dto.admin.order.OrderQuery;
//=====order.dto.api.order.*;
import com.chushouya.order.service.api.OrderApiService;
import com.chushouya.product.dto.api.evaluate.EvaluateResultItemApiDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

@Service
@Slf4j
public class OrderApiServiceImpl implements OrderApiService {

    @Resource
    private OrderRepository orderRepository;

    @Resource
    private OrderProductRepository orderProductRepository;

    @Resource
    private OrderAddressRepository orderAddressRepository;

    @Resource
    private OrderClothesProductRepository orderClothesProductRepository;

    private OrderEntity getOrder(String orderNo){
        OrderQuery query = new OrderQuery();
        query.setOrderNo(orderNo);
        final OrderEntity orderEntity = orderRepository.selectOne(query);
        Check.notNull(orderEntity, "订单不存在");
        return orderEntity;
    }

    @Override
    public PageResult<OrderListApiDTO> userOrderSearch(OrderApiQuery query) {
        OrderQuery orderQuery = Beans.copy(query, OrderQuery.class);
        orderQuery.setOrderStatusList(UserOrderListLogicStatusEnum.get(query.getStatus()).statusList());
        final Long userId = Contexts.getUserContext().getUserId();
        orderQuery.setUserId(userId);
//        orderQuery.setBizType(BizTypeEnum.SHUMA.value());
        PageResult<OrderEntity> orderPage = orderRepository.selectPageResult(orderQuery);
        return PageResult.of(orderPage, OrderListApiDTO.class);
    }

    @Override
    public OrderInfoApiDTO userOrderDetail(String orderNo) {
        final OrderEntity orderEntity = this.getOrder(orderNo);
        final Long orderId = orderEntity.getOrderId();
        final Integer bizType = orderEntity.getBizType();
        final OrderInfoApiDTO order = Beans.copy(orderEntity, OrderInfoApiDTO.class);
        order.setOrderAddress(this.orderAddress(orderId));
        if(Objects.equals(BizTypeEnum.JIUYI.value(), bizType)){
            order.setOrderProduct(this.orderClotheProduct(orderId));
        }else{
            order.setOrderProduct(this.orderProduct(orderId));
        }
        return order;
    }

    @Override
    public void updatePartner(OrderUpdatePartnerApiDTO data) {
        final OrderEntity orderEntity = this.getOrder(data.getOrderNo());
        OrderEntity update = new OrderEntity();
        update.setOrderId(orderEntity.getOrderId());
        update.setOrderCenterNo(data.getOrderCenterNo());
        update.setOrderId(orderEntity.getOrderId());
        update.setPartnerId(100004L);
        update.setPartnerName("支付宝私域");
        orderRepository.updateByPrimaryKeySelective(update);
    }


    /**
     * 获取订单商品信息
     */
    private OrderEvaluateApiDTO orderProduct(Long orderId) {
        OrderProductEntity orderProductEntity = orderProductRepository.selectByOrderId(orderId);
        if (Objects.isNull(orderProductEntity)) {
            throw Ex.business("订单商品信息不存在");
        }
        OrderEvaluateApiDTO orderProductDTO = Beans.copy(orderProductEntity, OrderEvaluateApiDTO.class);
        orderProductDTO.setEvaluateItem(Jsons.parseArray(orderProductEntity.getEvaluateItem(), EvaluateResultItemApiDTO.class));
        orderProductDTO.setQualityItem(Jsons.parseArray(orderProductEntity.getQualityItem(), EvaluateResultItemApiDTO.class));
        return orderProductDTO;
    }

    /**
     * 订单地址信息
     */
    private OrderAddressApiDTO orderAddress(Long orderId) {

        OrderAddressEntity
                orderAddressEntity = orderAddressRepository.selectByOrderId(orderId);
        if (Objects.isNull(orderAddressEntity)) {
            throw Ex.business("订单地址信息不存在");
        }
        return Beans.copy(orderAddressEntity, OrderAddressApiDTO.class);
    }

    /**
     * 旧衣产品信息
     */
    private OrderClotheProductApiDTO orderClotheProduct(Long orderId) {
        OrderClothesProductEntity orderProductEntity = orderClothesProductRepository.selectByOrderId(orderId);
        if (Objects.isNull(orderProductEntity)) {
            throw Ex.business("订单商品信息不存在");
        }
        return Beans.copy(orderProductEntity, OrderClotheProductApiDTO.class);
    }


}
